import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:study_demo/ui/slide/controller.dart';
import 'package:flutter_slidable/flutter_slidable.dart';

class SlidePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetX<SlideController>(
        init: SlideController(),
        builder: (controller) {
          return Scaffold(
              appBar: AppBar(title: Text('侧滑删除')),
              body: Container(
                child: SlidableAutoCloseBehavior(
                  child: ListView.separated(
                    itemCount: controller.newsList.length,
                    itemBuilder: (context, index) {
                      return GestureDetector(
                        onTap: () {},
                        child: itemLayout(index, controller),
                      );
                    },
                    separatorBuilder: (context, index) {
                      return Divider(
                        height: 3,
                        color: Colors.transparent,
                      );
                    },
                  ),
                ),
              ));
        });
  }

  // 子条目布局
  itemLayout(int index, SlideController controller) {
    return Card(
      child: Slidable(
        key: Key(controller.newsList[index].D_Title!),
        endActionPane:
            ActionPane(extentRatio: 0.2, motion: ScrollMotion(), children: [
          SlidableAction(
            flex: 1,
            onPressed: (_) {
              controller.deleteItem(controller.newsList[index].D_ID!);
            },
            autoClose: true,
            backgroundColor: Colors.red,
            foregroundColor: Colors.white,
            label: '删除',
          ),
        ]),
        child: Container(
          alignment: Alignment.centerLeft,
          padding: EdgeInsets.fromLTRB(15, 0, 20, 0),
          width: double.infinity,
          height: 45,
          child: Text(controller.newsList[index].D_Title!),
        ),
      ),
    );
  }
}
